% This script performs Portfolio Optimizations and Risk Portfolio Analysis.
% For more details go to "www.gerardoferrara.com", by G. Ferrara.

clc; clear; close all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
RETURNS=xlsread('database.xls', 'Series','b2:i22')

[NUN, NUN1, NAMETEXT]=xlsread('database','Series','B1:I1');

EXPECTED_RETURNS=mean(RETURNS)


h=figure(1)
barh(EXPECTED_RETURNS)
Title('Expected Returns of Asset Calsses (1988-2008)')
ylabel('Asset Classes')
xlabel('Expected Return')




    EW=ones(1,8)*(1/8)


h=figure(2)
pie(EW)
Title('Portfolio Composition')


EXP_RET_PORT=EW*EXPECTED_RETURNS'


FINAL_RETURNS=[EXPECTED_RETURNS, EXP_RET_PORT]
h=figure(3)
barh(FINAL_RETURNS)
hold on
barh(EXPECTED_RETURNS,'r')
hold off
Title('Expected Returns of Asset Calsses/Portfolio (1988-2008)')
ylabel('Asset Classes/Portfolio')
xlabel('Expected Return')


h=figure(4)
for j=1:8
plot(RETURNS(:,j),'Color',[rand(1,1) rand(1,1) rand(1,1)])
hold on
end
grid on
Title('Volatility, graphical analysis')
legend1 = legend(NAMETEXT,'Location','SouthOutside')
hold off


A=min(min(RETURNS)')
B=max(max(RETURNS)')


h=figure(5)
for j=1:8
subplot(4,2,j)
plot(RETURNS(:,j))
hold on
plot(ones(21,1)* EXPECTED_RETURNS(1,j),':r')
grid on
Title('Volatility, graphical analysis')
hold off
ylim([A B])
end


ST_DEV=std(RETURNS)



h=figure(6)
barh(ST_DEV)
Title('Sigma of Asset Calsses (1988-2008)')
ylabel('Asset Classes')
xlabel('Sigma')



REND_SIGMA=[EXPECTED_RETURNS; ST_DEV]



h=figure(7)
barh(REND_SIGMA')
Title('Return-Sigma of Asset Calsses (1988-2008)')
ylabel('Asset Classes')
xlabel('Return-Sigma')



CORR=corr(RETURNS)



h=figure(8)
scatter(RETURNS(:,5), RETURNS(:,6))



h=figure(9)
scatter(RETURNS(:,7), RETURNS(:,8))




W_s=EW.*ST_DEV;

SIGMA_PORTFOLIO=sqrt(W_s*CORR*W_s')


CORR1=CORR;
CORR1(7,8)=0;
CORR1(8,7)=0;

SIGMA_PORTFOLIO1=sqrt(W_s*CORR1*W_s')



GAP_SQUARED=zeros(21,1);
for j=1:21
if RETURNS(j,8)-mean(RETURNS(:,8))<0    
GAP_SQUARED(j,1)=(RETURNS(j,8)-mean(RETURNS(:,8)))^2;
end
end
SEMI_SIGMA=sqrt(mean(GAP_SQUARED))


SEMI_VA=(mean(GAP_SQUARED))

SIGMA_POP=std(RETURNS(:,8),1)

VARIANCE=SIGMA_POP^2;


RATI0=SEMI_VA/VARIANCE;


SKEW=skewness(RETURNS);

KURT=kurtosis(RETURNS);



LIV_CONF=0.97

K = norminv(LIV_CONF,0,1)



VAR_ASSET_CLASSES=EXPECTED_RETURNS-K*ST_DEV;

h=figure(8)
barh(VAR_ASSET_CLASSES')
Title('Value at Risk of Asset Calsses (1988-2008)')
ylabel('Asset Classes')
xlabel('VaR')
grid on



VAR_PORTFOLIO=EXP_RET_PORT-K*SIGMA_PORTFOLIO;



Interest=zeros(22,1);
Interest(1,1)=100;

for i=2:22
Interest(i,1)=Interest(i-1,1)*(1+RETURNS(i-1,8));
end


Interest2=zeros(22,1);
Interest2(1,1)=100;

for i=2:22
Interest2(i,1)=Interest2(i-1,1)*(1+RETURNS(i-1,1));
end


h=figure(9)
plot(Interest)
hold on
plot(Interest2,'r')
hold off
Title('Principal and Interest')
ylabel('Principal and Interest')
xlabel('Time')
grid on





EXP_RET=[0.03 0.038 0.041 0.075 0.07 0.06 0.065 0.08];


CORR
ST_DEV

VAR_COV=corr2cov(ST_DEV,CORR);
[PortRisk, PortReturn, PortWts] = portopt(EXP_RET, VAR_COV,100) 



h=figure(10)
plot(PortRisk,PortReturn)
Title('Efficient Frontier')

h=figure(11)
area(PortWts)
Title('Composition of efficient portfolio')
legend1 = legend(NAMETEXT,'Location','SouthOutside')
ylim([0 1]);


%ConSet = portcons('PortValue', PVal, k,'AssetLims',AssetMin, AssetMax,'GroupComparison',GroupA,AtoBmin,AtoBmax ,GroupB,'Custom',D, E,'GroupLims', Group, GroupMin, GroupMax); 


% Measure the VaR of efficient portfolios
K = norminv(0.84,0,1)


VAR_EFF_PORTFOLIO=PortReturn-K*PortRisk;
h=figure(12)
bar(VAR_EFF_PORTFOLIO)
hold on
plot(-0.05*ones(100,1),'r')
hold off
Title('VaR versus threshold desired')
grid on


% With traditional contraints
AssetMin=xlsread('database', 'Limit','B2:B9')'
AssetMax=xlsread('database', 'Limit','C2:C9')'
ConSet = portcons('Default', 8,'AssetLims',AssetMin, AssetMax); 
[PortRisk1, PortReturn1, PortWts1] = portopt(EXP_RET, VAR_COV,100, [], ConSet) 

h=figure(12)
area(PortWts1)
Title('Composition of constrained efficient portfolio')
legend1 = legend(NAMETEXT,'Location','SouthOutside')
ylim([0 1]);



% With infra-group contraints
D=xlsread('database', 'Limit','B12:I21')
E=xlsread('database', 'Limit','K12:K21')
ConSet = portcons('Default',8,'Custom',D, E);
[PortRisk2, PortReturn2, PortWts2] = portopt(EXP_RET, VAR_COV,100, [], ConSet) 


h=figure(13)
area(PortWts2)
Title('Composition of INFRA-GR. constrained efficient portfolio')
legend1 = legend(NAMETEXT,'Location','SouthOutside')
ylim([0 1]);



